package com.ch.d0416;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class QQClient {
    public static void main(String[] args) throws IOException {

        // 创建即连接
        Socket socket = new Socket("127.0.0.1",8888);

        InputStream in= socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        Scanner sc = new Scanner(System.in);

        Thread t1 = new Thread( ()->{
            while(true){
                System.out.println("请输入:");
                int i = Integer.parseInt(sc.nextLine());
                try{
                    if (i==1){
                        String msg = sc.nextLine();
                        MsgUtil.sendText(msg,out);
                    }else if(i==2){
                        System.out.println("请输入文件路径");
                        String filepath = sc.nextLine();
                        MsgUtil.sendFile(filepath,out);
                    }
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread( ()-> {
            while(true){
                try {
                   String msg = MsgUtil.recv(in);
                    System.out.println("服务器:" + msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t2.start();

    }
}
